package com.pur.zsms.weixin.web;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 支付控制controller
 * 
 * @author linwenzhou
 * @date 2014-12-08
 */
@Controller
@RequestMapping("pay")
public class WeixinPayController
{

	private static Logger log = LoggerFactory.getLogger(WeixinPayController.class);
	private static String API_KEY = "你设置的商户支付密匙";
	private static String notify_url = "pay/pay.html"; // 支付的回调方法
	private static String uni_url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
	//@Autowired
	//private ShopOrdersService orderService;

	/**
	 * 跳转到支付购买页面
	 * 
	 * @param openId
	 *            微信粉丝群ID
	 * 
	 * @return
	 */
	/*@RequestMapping(value = "gopay")
	public @ResponseBody
	String Gopay(String openId, String time, HttpServletRequest request) {
		String path = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
		log.info("base path=" + basePath);
		log.info("go pay come in the openId=" + openId + "==time=" + time);
		SortedMap<Object, Object> parameters = new TreeMap<Object, Object>();
		parameters.put("appid", "wxb714cxxxx");
		parameters.put("mch_id", "1001xxx");
		parameters.put("nonce_str", hqjUtil.generateRandCode(32));
		parameters.put("body", "支付测试");// 购买支付信息
		parameters.put("out_trade_no", hqjUtil.generateRandCode(12));// 订单号
		parameters.put("total_fee", "1");// 总金额单位为分
		parameters.put("spbill_create_ip", hqjUtil.getIpAddr(request));
		parameters.put("notify_url", basePath + notify_url);
		parameters.put("trade_type", "JSAPI");
		parameters.put("openid", openId);
		String sign = createSign("UTF-8", parameters);
		parameters.put("sign", sign);
		String requestXML = getRequestXml(parameters);
		// log.info("requestXML" + requestXML);
		String result = CommonUtil.httpsRequest2(uni_url, "POST", requestXML);
		// log.info("result" + result);
		try {
			Map<String, String> map = XMLUtil.doXMLParse(result);
			log.info("map" + map);
			SortedMap<Object, Object> params = new TreeMap<Object, Object>();
			params.put("appId", "wxxxxxx");
			params.put("timeStamp", "\"" + new Date().getTime() + "\"");
			params.put("nonceStr", hqjUtil.generateRandCode(32));
			params.put("package", "prepay_id=" + map.get("prepay_id"));
			params.put("signType", "MD5");
			String paySign = createSign("UTF-8", params);
			params.put("packageValue", "prepay_id=" + map.get("prepay_id")); // 这里用packageValue是预防package是关键字在js获取值出错
			params.put("paySign", paySign); // paySign的生成规则和Sign的生成规则一致
			params.put("sendUrl", basePath + "mobile/paysucxx.xhtml?openId=" + openId + "&time=" + time); // 付款成功后跳转的
																											// 方法
			String userAgent = request.getHeader("user-agent");
			char agent = userAgent.charAt(userAgent.indexOf("MicroMessenger") + 15);
			params.put("agent", new String(new char[] { agent }));// 微信版本号，用于前面提到的判断用户手机微信的版本是否是5.0以上版本。
			String json = JSONArray.fromObject(params).toString();
			return json;
		} catch (JDOMException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}// 解析微信返回的信息，以Map形式存储便于取值
		return "";
	}

	*//**
	 * @author lwz
	 * @date 2014-12-8
	 * @Description：sign签名
	 * @param characterEncoding
	 *            编码格式
	 * @param parameters
	 *            请求参数
	 * @return
	 *//*
	public static String createSign(String characterEncoding, SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			Object v = entry.getValue();
			if (null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
				sb.append(k + "=" + v + "&");
			}
		}
		sb.append("key=" + API_KEY);
		String sign = MD5Util.MD5Encode(sb.toString(), characterEncoding).toUpperCase();
		return sign;
	}

	*//**
	 * @author 老妖
	 * @date 2014-12-5下午2:32:05
	 * @Description：将请求参数转换为xml格式的string
	 * @param parameters
	 *            请求参数
	 * @return
	 *//*
	public static String getRequestXml(SortedMap<Object, Object> parameters) {
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		Set es = parameters.entrySet();
		Iterator it = es.iterator();
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			String k = (String) entry.getKey();
			String v = (String) entry.getValue();
			if ("attach".equalsIgnoreCase(k) || "body".equalsIgnoreCase(k) || "sign".equalsIgnoreCase(k)) {
				sb.append("<" + k + ">" + "<![CDATA[" + v + "]]></" + k + ">");
			} else {
				sb.append("<" + k + ">" + v + "</" + k + ">");
			}
		}
		sb.append("</xml>");
		return sb.toString();
	}

	@RequestMapping(value = "pay")
	public @ResponseBody
	void success(HttpServletRequest request, HttpServletResponse response) throws IOException, JDOMException {
		InputStream inStream = request.getInputStream();
		ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = inStream.read(buffer)) != -1) {
			outSteam.write(buffer, 0, len);
		}
		log.info("~~~~~~~~~~~~~~~~付款成功~~~~~~~~~");
		outSteam.close();
		inStream.close();
		String result = new String(outSteam.toByteArray(), "utf-8");// 获取微信调用我们notify_url的返回信息
		Map<Object, Object> map = XMLUtil.doXMLParse(result);
		for (Object keyValue : map.keySet()) {
			log.info(keyValue + "=" + map.get(keyValue));
		}
		if (map.get("result_code").toString().equalsIgnoreCase("SUCCESS")) {
			// TODO 对数据库的操作
			response.getWriter().write(setXML("SUCCESS", "")); // 告诉微信服务器，我收到信息了，不要在调用回调action了
			log.info("-------------" + setXML("SUCCESS", ""));
		}
	}

	public static String setXML(String return_code, String return_msg) {
		return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA[" + return_msg + "]]></return_msg></xml>";
	}

	@RequestMapping(value = "paysuc")
	public String paysuc(String openId, String time) {
		ShopOrders order = orderService.findByOpenIdTime(openId, time);
		order.setOrderStatus(1);
		order.setOuttime(hqjUtil.getTimesmmss());
		orderService.updateStatus(order);
		return "redirect:/mobile/mallwwew.wwhtml?openId=" + openId;
	}*/

}